package com.yuanlp.shoppingmall.home.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yuanlp.shoppingmall.R;
import com.yuanlp.shoppingmall.home.model.ResultBeanData;
import com.yuanlp.shoppingmall.utils.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 原立鹏 on 2017/8/18.
 *
 * 频道的适配器，类似于listview的适配器
 */

public class ChannelAdapter extends BaseAdapter {

    Context mContext;
    ArrayList<ResultBeanData.ResultBean.ChannelInfoBean> list;
    public ChannelAdapter(Context context, List<ResultBeanData.ResultBean.ChannelInfoBean> channel_info){
        this.mContext=context;
        this.list= (ArrayList<ResultBeanData.ResultBean.ChannelInfoBean>) channel_info;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=new ViewHolder();
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.item_channel,null);  //子布局，类似于listview的每个行的布局一样
            viewHolder.iv_channel= (ImageView) convertView.findViewById(R.id.iv_channel);
            viewHolder.tv_channel= (TextView) convertView.findViewById(R.id.tv_channel);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        ResultBeanData.ResultBean.ChannelInfoBean channelInfoBean=list.get(position);
        //利用glide加载远程图片到本地图片上
        Glide.with(mContext).load(Constants.IMG_URL+channelInfoBean.getImage()).into(viewHolder.iv_channel);
        viewHolder.tv_channel.setText(channelInfoBean.getChannel_name());
        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    //根据子布局，里面的控件类型，做一个内部的viewholder
    static class ViewHolder{
        ImageView iv_channel;
        TextView tv_channel;

    }

}
